package com.jiaai.shop.store.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jiaai.shop.store.entity.SExtractMoney;
import com.jiaai.shop.store.entity.SMember;
import com.jiaai.shop.store.mapper.SMemberMapper;
import com.jiaai.shop.store.service.ISMemberService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jiaai.shop.util.PageUtil;
import com.jiaai.wgj.common.page.RPage;
import com.jiaai.wgj.util.exception.LoginException;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.stereotype.Service;

import javax.persistence.Convert;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author
 * @since 2018-04-01
 */
@Service
public class SMemberServiceImpl extends ServiceImpl<SMemberMapper, SMember> implements ISMemberService {

    @Override
    public Object updateeturnIntegral(int Integral,int lv,String mId) {
        return baseMapper.updateeturnIntegral(Integral,lv,mId);
    }

    @Override
    public boolean updateTotalScore(int sorce, String mId) {
        return baseMapper.updateTotalScore(sorce,mId);
    }

    @Override
    public List<Map> seachAllMember() {
        return baseMapper.seachAllMember();
    }

    @Override
    public int seachInter(String mId) {
        return baseMapper.seachInter(mId);
    }

    @Override
    public RPage<Object> seachMember(Page page ,Wrapper wrapper) {
        page.setRecords(baseMapper.seachMember(page,wrapper));
        return new PageUtil<Object>().convertRpage(page);
    }


    @Override
    public void updateMenberbyId(SExtractMoney sExtractMoney)throws  Exception {
        SMember sMember= baseMapper.selectById(sExtractMoney.getMemberId());
        if(sMember.getPresentScore()<Integer.valueOf(sExtractMoney.getExtractNumber())){
                throw  new Exception("积分不足");
        }
        sMember.setPresentScore(sMember.getPresentScore()-Integer.parseInt(sExtractMoney.getExtractNumber()));
        sMember.setScore(sMember.getScore()+Integer.parseInt(sExtractMoney.getExtractNumber()));
        baseMapper.updateById(sMember);
    }

    @Override
    public void upMenberbyId(SExtractMoney sExtractMoney) {
        SMember sMember= new SMember();
        sMember.setMemberId(sExtractMoney.getMemberId());
        sMember =baseMapper.selectById(sMember);
        sMember.setPresentScore(sMember.getPresentScore()+ ((int)Double.parseDouble(sExtractMoney.getExtractNumber())));
        sMember.setScore(sMember.getScore()-((int)Double.parseDouble(sExtractMoney.getExtractNumber())));
        baseMapper.updateById(sMember);
    }
}
